﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// .net core处理请求类
    /// </summary>
    public class UniAppDetailsHelper
    {

        /// <summary>
        /// 根据表名创建Index管理页面
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateUniAppDetailsClass(string strFilePath,string strProjectName, string strTableName,string strTableComment,string strDBName)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            Directory.CreateDirectory(strFilePath);
            //为每个表创建一个独立目录存放
            string strTableNamePath = strFilePath + "\\"+ strClassName;
            if (!Directory.Exists(strTableNamePath))
            {
                Directory.CreateDirectory(strTableNamePath);
            }
            StreamWriter sw = new StreamWriter(strTableNamePath + "\\details.vue", false, Encoding.GetEncoding("utf-8"));//生成物理文件
            string strTableComment2 = CommonHelper.GetColumnKeyComment(strTableComment);

            sw.WriteLine("<template>");
            sw.WriteLine("	<view class=\"u-skeleton\">");
            sw.WriteLine("		<view class=\"details\">");
            sw.WriteLine("			<view class=\"details_title u-text-left u-p-t-40 u-font-34 u-main-color u-skeleton-fillet\">");
            sw.WriteLine("				<text>{{data."+ strTableNameLower + "_title}}</text>");
            sw.WriteLine("			</view>");
            sw.WriteLine("			<view class=\"details_title_button u-m-t-30 u-skeleton-fillet\">");
            sw.WriteLine("				<u-row gutter=\"10\">");
            sw.WriteLine("					<u-col span=\"6\">");
            sw.WriteLine("						<view class=\"u-text-left u-tips-color\">来源：<text>{{data.source_name}}</text>");
            sw.WriteLine("						</view>");
            sw.WriteLine("					</u-col>");
            sw.WriteLine("					<u-col span=\"6\">");
            sw.WriteLine("						<view class=\"u-text-right u-tips-color\"><text>发布日期：{{data.created_at}}</text></view>");
            sw.WriteLine("					</u-col>");
            sw.WriteLine("				</u-row>");
            sw.WriteLine("				<view class=\"u-p-t-30\">");
            sw.WriteLine("					<u-line color=\"#f2f2f2\" length=\"100%\" />");
            sw.WriteLine("				</view>");
            sw.WriteLine("			</view>");
            sw.WriteLine("			<view class=\"details_info u-m-t-30 u-skeleton-fillet\">");
            sw.WriteLine("				<!-- 内容详情 -->");
            sw.WriteLine("				<u-parse :html=\"data." + strTableNameLower + "_content\" class=\"edit\"></u-parse>");
            sw.WriteLine("			</view>");
            sw.WriteLine("			<!-- 无网络提示组件 -->");
            sw.WriteLine("			<u-no-network></u-no-network>");
            sw.WriteLine("		</view>");
            sw.WriteLine("		<!-- 骨架屏组件 -->");
            sw.WriteLine("		<u-skeleton :loading=\"loading\" :animation=\"true\" bgColor=\"#fff\"></u-skeleton>");
            sw.WriteLine("	</view>");
            sw.WriteLine("</template>");
            sw.WriteLine("");
            sw.WriteLine("<script>");
            sw.WriteLine("	export default {");
            sw.WriteLine("		onLoad(option) {");
            sw.WriteLine("			this.id = option.id;");
            sw.WriteLine("			this.detailsData();");
            sw.WriteLine("		},");
            sw.WriteLine("		data() {");
            sw.WriteLine("			return {");
            sw.WriteLine("				loading: true,");
            sw.WriteLine("				id: '0',");
            sw.WriteLine("				data: {");
            sw.WriteLine("					" + strTableNameLower + "_content: ''");
            sw.WriteLine("				},");
            sw.WriteLine("			}");
            sw.WriteLine("		},");
            sw.WriteLine("		onShareAppMessage() {");
            sw.WriteLine("");
            sw.WriteLine("		},");
            sw.WriteLine("		methods: {");
            sw.WriteLine("");
            sw.WriteLine("			/**");
            sw.WriteLine("			 * 根据ID获取详情页面数据");
            sw.WriteLine("			 */");
            sw.WriteLine("			detailsData() {");
            sw.WriteLine("				this.$u.get('/api/" + strTableNameLower + "/' + this.id).then(res => {");
            sw.WriteLine("					if (res.code == 0) {");
            sw.WriteLine("						console.log('data=' + JSON.stringify(res.data));");
            sw.WriteLine("						this.data = res.data;");
            sw.WriteLine("						this.loading = false");
            sw.WriteLine("					} else {");
            sw.WriteLine("						this.loading = true");
            sw.WriteLine("					}");
            sw.WriteLine("				})");
            sw.WriteLine("			},");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("	}");
            sw.WriteLine("</script>");
            sw.WriteLine("");
            sw.WriteLine("<style scoped lang=\"scss\">");
            sw.WriteLine("	.edit {");
            sw.WriteLine("		min-height: 20vh;");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	.details {");
            sw.WriteLine("		padding-bottom: 30rpx;");
            sw.WriteLine("");
            sw.WriteLine("		.details_title {");
            sw.WriteLine("			width: 94%;");
            sw.WriteLine("			margin: auto;");
            sw.WriteLine("			font-weight: bold;");
            sw.WriteLine("		}");
            sw.WriteLine("");
            sw.WriteLine("		.details_title_button {");
            sw.WriteLine("			width: 94%;");
            sw.WriteLine("			margin: auto;");
            sw.WriteLine("		}");
            sw.WriteLine("");
            sw.WriteLine("		.details_info {");
            sw.WriteLine("			width: 94%;");
            sw.WriteLine("			margin: auto;");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("</style>");


            sw.Close();

        }

    }
}
